<?php

class User extends AppModel {
	var $name = 'User';

	var $useTable = 'mnhis_user';

	protected $fields = array(
		'id' => array('name' => 'ID'),
		'username' => array('name' => 'Používateľské meno / identifikátor'),
		'password' => array('name' => 'Heslo'),
		'fk_person_id' => array('name' => 'Osoba', 'reference' => array('model' => 'Person', 'field' => 'full_name')),
		'restricted_access' => array('name' => 'Obmedzený prístup', 'evaluation' => array('0' => 'nie', '1' => 'áno', 'default' => 'nie')),
	);
	
	var $belongsTo = array(
		'Person' => array(
			'className' => 'Person',
			'foreignKey' => 'fk_person_id',
		),
	);

	var $validate = array(
		'username' => array(
			'required' => array(
				'rule' => array('minLength', 1),
				'required' => TRUE,
				'message' => 'Musíte vyplniť používateľské meno',
			),
			'unique' => array(
				'rule' => 'isUnique',
				'message' => 'Používateľské meno sa už používa.'
			),
		),
		'fk_person_id' => array(
			'rule' => array('comparison', '>', 0),
			'message' => 'Musíte vybrať osobu'
		)
	);

	public function beforeSave() {
		parent::beforeSave();
		if (isset($this->data[$this->alias]['password'])) {
			$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
		}
		return TRUE;
	}
}

?>